package net.boss.fang.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RedisTestController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    
    @RequestMapping("redis/test")
    public void setNX() {
        Boolean flag = redisTemplate.execute(new RedisCallback<Boolean>() {

            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                byte[] key = redisTemplate.getStringSerializer().serialize("name");
                byte[] name = redisTemplate.getStringSerializer().serialize("fang cheng huang");
                System.out.println(connection.del(key));
                connection.setNX(key, name);
                //nameV = redisTemplate.getStringSerializer().deserialize(name);
                //System.out.println("name:"+nameV);
                return true;
            }
        });
    }
}
